
# 练习encode\decode
contents = '我在剑桥通常指导本科生时,只要在课程上比他们提前一周预看即可。'

contents_gbk = contents.encode(encoding='gbk')
contents_utf = contents.encode(encoding='utf-8')

print(contents_gbk)
b'\xce\xd2\xd4\xda\xbd\xa3\xc7\xc5\xcd\xa8\xb3\xa3\xd6\xb8\xb5\xbc\xb1\xbe\xbf\xc6\xc9\xfa\xca\xb1,\xd6\xbb\xd2\xaa\xd4\xda\xbf\xce\xb3\xcc\xc9\xcf\xb1\xc8\xcb\xfb\xc3\xc7\xcc\xe1\xc7\xb0\xd2\xbb\xd6\xdc\xd4\xa4\xbf\xb4\xbc\xb4\xbf\xc9\xa1\xa3'
print(contents_utf)
b'\xe6\x88\x91\xe5\x9c\xa8\xe5\x89\x91\xe6\xa1\xa5\xe9\x80\x9a\xe5\xb8\xb8\xe6\x8c\x87\xe5\xaf\xbc\xe6\x9c\xac\xe7\xa7\x91\xe7\x94\x9f\xe6\x97\xb6,\xe5\x8f\xaa\xe8\xa6\x81\xe5\x9c\xa8\xe8\xaf\xbe\xe7\xa8\x8b\xe4\xb8\x8a\xe6\xaf\x94\xe4\xbb\x96\xe4\xbb\xac\xe6\x8f\x90\xe5\x89\x8d\xe4\xb8\x80\xe5\x91\xa8\xe9\xa2\x84\xe7\x9c\x8b\xe5\x8d\xb3\xe5\x8f\xaf\xe3\x80\x82'

origin_gbk = contents_gbk.decode(encoding='gbk')
origin_utf = contents_utf.decode(encoding='utf-8')

print(origin_gbk)
我在剑桥通常指导本科生时,只要在课程上比他们提前一周预看即可。
print(origin_utf)
我在剑桥通常指导本科生时,只要在课程上比他们提前一周预看即可。

#练习CRUD
#creat\retrieve
content_1 = 'deliberate practice'
content_1 += ',guy!'
print(content_1)
deliberate practice,guy!

content_1[5]
'e'
content_1.find('guy')
20
content_1.index('guy')
20
content_1.startswith('del')
True
content_1.endswith('guy!')
True

#update\delete
content_1.replace('guy','bro')
'deliberate practice,bro!'
print(content_1.split( ))
['deliberate', 'practice,guy!']
content_1 = 'deliberate practice,bro!'

t1 = ''
content_2 = ('hello','bro!')
print(t1.join(content_2))
hellobro!

content_3 = '***hello,bro***'
print(content_3.strip('*'))
hello,bro

#字符串的输入和输出
files = open('file.text','w',encoding='utf-8')
content_4 = 'hello,girl'
files.write(content_4)
10
files.close()

fr = open('file.text','r',encoding='utf-8')
content_4 = fr.read()
print(content_4)
hello,girl

fra = open('file.text','a')
fra.write(',nice!')
6
fra.close()

#format

s1 = 'what the fuck?'
s2 = 'prick!'
print(f'A:{s1} , B:{s2}')
A:what the fuck? , B:prick!

print('you say:{s1}'.format(s1='what the fuck?'))
you say:what the fuck?

print('{:.3f}'.format(6.938319))
6.938

print('you are saying %s' %('I am prick?'))
you are saying I am prick?

